//
//  BRAgreementView.m
//  BrezzeSaaS
//
//  Created by IOS on 2022/3/3.
//  Copyright © 2022 Brezze GWG. All rights reserved.
//

#import "BRAgreementView.h"

@interface BRAgreementView ()<QMUITextViewDelegate>

@end

@implementation BRAgreementView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setUI];
    }
    return self;
}


- (void)btnClick:(QMUIButton *)btn {
    NSInteger i = btn.tag - 100;
    if (i == 0) {
        exit(0);
        return;
    }
    
    
    
}

+ (void)setIsAgree:(BOOL)isAgree {
    NSString *key = [NSString stringWithFormat:@"%@_%@_%@",kPlatormAgreeKey,USERManager.tenantModel.platformId,USERManager.userModel.userID];
    
    [[NSUserDefaults standardUserDefaults] setBool:isAgree forKey:key];
    [[NSUserDefaults  standardUserDefaults] synchronize];
}

+ (BOOL)isAgree {
    
    NSString *key = [NSString stringWithFormat:@"%@_%@_%@",kPlatormAgreeKey,USERManager.tenantModel.platformId,USERManager.userModel.userID];
    if ([[NSUserDefaults standardUserDefaults] objectForKey:key]) {
        return [[NSUserDefaults standardUserDefaults] boolForKey:key];
    }
    return NO;
}

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction {
    
    NSLog(@"%@",[URL scheme]);
    
    return YES;
}

- (void)setUI {
    
    self.backgroundColor = UIColorShadow;
    CGFloat margin_x = 16.;
    UIView *centView = [UIView new];
    centView.backgroundColor = UIColorWhite;
    [self addSubview:centView];
    centView.layer.cornerRadius = kCorners;
    centView.layer.masksToBounds = YES;
    [centView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.offset(0);
        make.centerY.offset(0);
        
    }];
    
    QMUILabel *title = [[QMUILabel alloc] init];
    title.font = [UIFont br_fontB:17];
    title.text = @"user term of use";
    title.textAlignment = NSTextAlignmentCenter;
    title.numberOfLines = 0;
    [centView addSubview:title];
    [title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(12);
        make.left.offset(margin_x);
        make.right.offset(-margin_x);
    }];
    
    UIView *line = [BRToolsView getLineView];
    [centView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(margin_x);
        make.right.offset(-margin_x);
        make.height.mas_equalTo(1);
        make.width.mas_equalTo(280);
        make.top.equalTo(title.mas_bottom).offset(20);
    }];
    
    QMUITextView *textView = [[QMUITextView alloc] init];
    textView.font = [UIFont br_fontR:16];
    textView.textColor = UIColor.qd_titleTextColor;
    [centView addSubview:textView];
    [textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(line.mas_bottom).offset(margin_x);
        make.left.offset(margin_x);
        make.right.offset(-margin_x);
        make.bottom.offset(-76);
        make.height.mas_equalTo(200);
    }];
    
    NSString *text = @"Clear WeChat Pay Balance WeChat Pay have been canceled (or was not activated); your account has no fund to be settled Deletion of vouchers in Cards & Offers and revocation of permissions on WeChat platform Vouchers in the Cards & Offers section of the WeChat account have been deleted. Administrator status for WeChat Official accounts (including Subscription Account, Service Account, and Mini Program), WeCom, and WeChat Open Platform has been revoked.";
    NSString *text2 = Localized(@"set_terms");
    NSString *text3 = Localized(@"set_privacy");
    NSString *textAll = [NSString stringWithFormat:@"%@\n<%@> <%@>\n",text,text2,text3];
    
    textView.text = textAll;
    textView.editable = NO;
    textView.delegate = self;
//    textView.is
    
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:textAll];
    
    str.yy_font = [UIFont br_fontE:16];
    [str yy_setAttribute:NSLinkAttributeName value:@"bodTerms1://" range:[textAll rangeOfString:text2]];
    [str yy_setAttribute:NSLinkAttributeName value:@"bodTerms2://" range:[textAll rangeOfString:text3]];
    [str yy_setAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:[textAll rangeOfString:text2]];
    [str yy_setAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:[textAll rangeOfString:text3]];
    str.yy_lineSpacing = 6;
    
    
    textView.attributedText = str;
    
    
    NSArray *texts = @[@"Close",@"Agree"];
    for (int i = 0; i < texts.count; i++) {
        QMUIButton *btn = [[QMUIButton alloc] init];
        [btn setTitle:texts[i] forState:UIControlStateNormal];
        [btn setTitleColor:UIColorWhite forState:UIControlStateNormal];
        UIColor *bColor = UIColor.qd_tintColor;
        if (i == 0) {
            bColor = UIColorGray;
        }
        [btn setBackgroundColor:bColor];
        btn.layer.cornerRadius = 4;
        btn.layer.masksToBounds = YES;
        [centView addSubview:btn];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset((140 * i) +  40);
            make.width.mas_equalTo(100);
            make.height.mas_equalTo(40);
            make.bottom.offset(-16);
        }];
        btn.tag = 100+i;
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
}


+ (void)requestConfigs:(void(^)(void))succ faile:(void(^)(void))faile {
    
    // 接口已废弃，注释掉调用
    /*
    NSString *urlstr = [NSString stringWithFormat:@"%@/123",[BRAPI configs]];
    
    [[BRRequestManager shared] GET:urlstr params:nil succeed:^(NSDictionary * _Nonnull dataDict, NSInteger respCode) {
        NSArray *configs = [BRConfigModel mj_objectArrayWithKeyValuesArray:dataDict[@"data"]];
        
        if (succ) {
            succ();
        }
        
    } failed:^(NSInteger code, NSString * _Nonnull errMsg) {
        if (faile) {
            faile();
        }
    }];
    */
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
